﻿using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace SkyDriveConnectorForSharePoint.SkyDriveConnector
{
    [ToolboxItem(false)]
    public partial class SkyDriveConnector : System.Web.UI.WebControls.WebParts.WebPart
    {
        /// <summary>
        /// Live Application Client ID
        /// </summary>
        [WebBrowsable(true),
        Category("SkyDrive Connector Properties"),
        DefaultValue(""),
        Personalizable(PersonalizationScope.Shared),
        WebDisplayName("Live Application Client Id"),
        WebDescription("")]
        public string ClientId
        {
            get;
            set;
        }

        /// <summary>
        /// Live Application Redirect Url
        /// </summary>
        [WebBrowsable(false),
        Category("SkyDrive Connector Properties"),
        DefaultValue(""),
        Personalizable(PersonalizationScope.Shared),
        WebDisplayName("Live Application Redirect Url"),
        WebDescription("")]
        public string RedirectUrl
        {
            get;
            set;
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            InitializeControl();

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            hidClientId.Value = ClientId;
            hidRedirectUrl.Value = SPContext.Current.Web.Url + "/Lists/SkyDriveConnectorConfig/CallBack.aspx";
            hidSiteCollectionUrl.Value = SPContext.Current.Web.Url;


            LiteralControl script = new LiteralControl();
            script.Text = "<script src='" + SPContext.Current.Web.Url + "/Lists/SkyDriveConnectorConfig/Silverlight.js" + "'></script> ";
            this.Controls.Add(script);
            ConfigureListView();

        }


        private void ConfigureListView()
        {
            SPView defaultview = SPContext.Current.Web.Lists["SkyDriveList"].DefaultView;            

            bool updaterequired = false;

            if (!defaultview.ViewFields.Exists("DocIcon"))
            {
                updaterequired = true;
                defaultview.ViewFields.Add("DocIcon");
            }
            if (!defaultview.ViewFields.Exists("SkyDriveObjectId"))
            {
                updaterequired = true;
                defaultview.ViewFields.Add("SkyDriveObjectId");
            }

            if (!defaultview.ViewFields.Exists("SkyDriveFileUrl"))
            {
                updaterequired = true;
                defaultview.ViewFields.Add("SkyDriveFileUrl");
            }

            if (!defaultview.ViewFields.Exists("ID"))
            {
                updaterequired = true;
                defaultview.ViewFields.Add("ID");

            }

            if (updaterequired)
            {
                SPContext.Current.Web.AllowUnsafeUpdates = true;
                defaultview.Update();

            }

        }

    }
}
